local assets =
{
    Asset("ANIM", "anim/bboy_candle.zip"),
    Asset("ANIM", "anim/bboy_candle_item.zip"),
}

local prefabs =
{
    "portable_campfirefire",
    "bboy_candle_item",
}

local prefabs_item =
{
    "bboy_candle01",
}

local function ChangeToItem(inst)
    local item = SpawnPrefab("bboy_candle_item")
    item.Transform:SetPosition(inst.Transform:GetWorldPosition())
    -- item.AnimState:PlayAnimation("collapse")
    item.SoundEmitter:PlaySound("meta5/walter/portable_fireplace_collapse")
    inst:Remove()
end

local function onhammered(inst, worker)
    local num_loots = inst.prefab == "bboy_candle02" and 2 or 3
    for i = 1, num_loots do
        inst.components.lootdropper:SpawnLootPrefab("bboy_candle_item")
    end

    inst:Remove()
end

local function onpostdeploy(inst)
    inst.deploytask = nil
    inst:RemoveTag("NOCLICK")
end

local function onhit(inst, worker)
    -- inst.AnimState:PlayAnimation("hit")
    -- inst.AnimState:PushAnimation("idle", false)
    if inst.deploytask then
        inst.deploytask:Cancel()
        onpostdeploy(inst)
    end
end

local function OnInit(inst)
    inst.components.burnable:FixFX()
end

local function SetShow(inst)
    local item02 = SpawnPrefab("bboy_candlefire")

    -- 若没有 Follower 组件，则手动加
    if item02.Follower == nil then
        item02.entity:AddFollower()
    end
    item02.entity:SetParent(inst.entity)
    item02.Follower:FollowSymbol(inst.GUID, "firefx02", nil, nil, nil, true)

    if inst.prefab == "bboy_candle03" then
        local item03 = SpawnPrefab("bboy_candlefire")

        -- 若没有 Follower 组件，则手动加
        if item03.Follower == nil then
            item03.entity:AddFollower()
        end
        item03.entity:SetParent(inst.entity)
        item03.Follower:FollowSymbol(inst.GUID, "firefx03", nil, nil, nil, true)
    end
end

local function OnSave(inst, data)
end

local function OnLoad(inst, data)
end

local function fn01()
    local inst = CreateEntity()

    inst.entity:AddTransform()
    inst.entity:AddAnimState()
    inst.entity:AddSoundEmitter()
    inst.entity:AddMiniMapEntity()
    inst.entity:AddNetwork()

    inst:SetDeploySmartRadius(DEPLOYSPACING_RADIUS[DEPLOYSPACING.ONEPOINTFIVE] / 2) -- item deployspacing/2
    inst:SetPhysicsRadiusOverride(0.1)
    MakeObstaclePhysics(inst, inst.physicsradiusoverride)

    inst.MiniMapEntity:SetIcon("bboy_candle.png")
    inst.MiniMapEntity:SetPriority(1)

    inst.AnimState:SetBank("bboy_candle")
    inst.AnimState:SetBuild("bboy_candle")
    inst.AnimState:PlayAnimation("idle01")
    inst.AnimState:SetBloomEffectHandle("shaders/anim.ksh")

    -- inst:AddTag("campfire")          -- 篝火
    inst:AddTag("structure")         -- 建筑
    inst:AddTag("wildfireprotected") -- 野火保护
    -- inst:AddTag("portable_campfire") -- 便携式篝火

    inst.entity:SetPristine()

    if not TheWorld.ismastersim then
        return inst
    end

    inst:AddComponent("portablestructure")
    inst.components.portablestructure:SetOnDismantleFn(ChangeToItem)

    inst:AddComponent("inspectable")

    inst:AddComponent("burnable")
    inst.components.burnable:AddBurnFX("bboy_candlefire", Vector3(0, 0, 0), "firefx01", true, nil, true)
    inst.components.burnable:Ignite(nil, nil, nil)
    inst.components.burnable:SetFXLevel(1)

    inst:AddComponent("lootdropper")
    inst:AddComponent("workable")
    inst.components.workable:SetWorkAction(ACTIONS.HAMMER)
    inst.components.workable:SetWorkLeft(2)
    inst.components.workable:SetOnFinishCallback(ChangeToItem)
    inst.components.workable:SetOnWorkCallback(onhit)

    MakeHauntableWork(inst)

    inst.OnSave = OnSave
    inst.OnLoad = OnLoad

    inst:DoTaskInTime(0, OnInit)

    return inst
end

local function fn02()
    local inst = CreateEntity()

    inst.entity:AddTransform()
    inst.entity:AddAnimState()
    inst.entity:AddSoundEmitter()
    inst.entity:AddMiniMapEntity()
    inst.entity:AddNetwork()

    inst:SetDeploySmartRadius(DEPLOYSPACING_RADIUS[DEPLOYSPACING.ONEPOINTFIVE] / 2) -- item deployspacing/2
    inst:SetPhysicsRadiusOverride(0.15)
    MakeObstaclePhysics(inst, inst.physicsradiusoverride)

    inst.MiniMapEntity:SetIcon("bboy_candle.png")
    inst.MiniMapEntity:SetPriority(1)

    inst.AnimState:SetBank("bboy_candle")
    inst.AnimState:SetBuild("bboy_candle")
    inst.AnimState:PlayAnimation("idle02")
    inst.AnimState:SetBloomEffectHandle("shaders/anim.ksh")

    inst:AddTag("structure")         -- 建筑
    inst:AddTag("wildfireprotected") -- 野火保护

    inst.entity:SetPristine()

    if not TheWorld.ismastersim then
        return inst
    end

    inst:AddComponent("inspectable")

    inst:AddComponent("burnable")
    inst.components.burnable:AddBurnFX("bboy_candlefire", Vector3(0, 0, 0), "firefx01", true, nil, true)
    inst.components.burnable:Ignite(nil, nil, nil)
    inst.components.burnable:SetFXLevel(1)

    inst:AddComponent("lootdropper")
    inst:AddComponent("workable")
    inst.components.workable:SetWorkAction(ACTIONS.HAMMER)
    inst.components.workable:SetWorkLeft(2)
    inst.components.workable:SetOnFinishCallback(onhammered)
    inst.components.workable:SetOnWorkCallback(onhit)

    MakeHauntableWork(inst)

    inst.OnSave = OnSave
    inst.OnLoad = OnLoad

    inst:DoTaskInTime(0, OnInit)
    inst:DoTaskInTime(0.1, SetShow)

    return inst
end

local function fn03()
    local inst = CreateEntity()

    inst.entity:AddTransform()
    inst.entity:AddAnimState()
    inst.entity:AddSoundEmitter()
    inst.entity:AddMiniMapEntity()
    inst.entity:AddNetwork()

    inst:SetDeploySmartRadius(DEPLOYSPACING_RADIUS[DEPLOYSPACING.ONEPOINTFIVE] / 2) -- item deployspacing/2
    inst:SetPhysicsRadiusOverride(0.15)
    MakeObstaclePhysics(inst, inst.physicsradiusoverride)

    inst.MiniMapEntity:SetIcon("bboy_candle.png")
    inst.MiniMapEntity:SetPriority(1)

    inst.AnimState:SetBank("bboy_candle")
    inst.AnimState:SetBuild("bboy_candle")
    inst.AnimState:PlayAnimation("idle03")
    inst.AnimState:SetBloomEffectHandle("shaders/anim.ksh")

    inst:AddTag("structure")         -- 建筑
    inst:AddTag("wildfireprotected") -- 野火保护

    inst.entity:SetPristine()

    if not TheWorld.ismastersim then
        return inst
    end

    inst:AddComponent("inspectable")

    inst:AddComponent("burnable")
    inst.components.burnable:AddBurnFX("bboy_candlefire", Vector3(0, 0, 0), "firefx01", true, nil, true)
    inst.components.burnable:Ignite(nil, nil, nil)
    inst.components.burnable:SetFXLevel(1)

    inst:AddComponent("lootdropper")
    inst:AddComponent("workable")
    inst.components.workable:SetWorkAction(ACTIONS.HAMMER)
    inst.components.workable:SetWorkLeft(2)
    inst.components.workable:SetOnFinishCallback(onhammered)
    inst.components.workable:SetOnWorkCallback(onhit)

    MakeHauntableWork(inst)

    inst.OnSave = OnSave
    inst.OnLoad = OnLoad

    inst:DoTaskInTime(0, OnInit)
    inst:DoTaskInTime(0.1, SetShow)

    return inst
end

--------------------------------------------------------------------------

local function ondeploy(inst, pt, deployer)
    local firepit = SpawnPrefab("bboy_candle01")
    if firepit then
        firepit.Physics:SetCollides(false)
        firepit.Physics:Teleport(pt.x, 0, pt.z)
        firepit.Physics:SetCollides(true)
        firepit.AnimState:PlayAnimation("idle01")
        -- firepit.AnimState:PushAnimation("idle", false)
        firepit.SoundEmitter:PlaySound("meta5/walter/portable_fireplace_place")
        firepit:AddTag("NOCLICK")
        firepit.deploytask = firepit:DoTaskInTime(13 * FRAMES, onpostdeploy)
        inst:Remove()
        PreventCharacterCollisionsWithPlacedObjects(firepit)
    end
end

local function item_OnSave(inst, data)
end

local function item_OnLoad(inst, data)
end

local FLOATABLE_SCALE = { 1.3, 0.9, 1.3 }

local function itemfn()
    local inst = CreateEntity()

    inst.entity:AddTransform()
    inst.entity:AddAnimState()
    inst.entity:AddSoundEmitter()
    inst.entity:AddNetwork()

    MakeInventoryPhysics(inst)

    inst.AnimState:SetBank("bboy_candle_item")
    inst.AnimState:SetBuild("bboy_candle_item")
    inst.AnimState:PlayAnimation("idle")

    inst:AddTag("portableitem")
    inst:AddTag("bboy_candle")

    MakeInventoryFloatable(inst, "small", .15, FLOATABLE_SCALE)

    inst.entity:SetPristine()

    if not TheWorld.ismastersim then
        return inst
    end

    inst:AddComponent("stackable")
    inst.components.stackable.maxsize = TUNING.STACK_SIZE_MEDITEM

    inst:AddComponent("inspectable")

    inst:AddComponent("inventoryitem")

    inst:AddComponent("deployable")
    inst.components.deployable.ondeploy = ondeploy
    inst.components.deployable:SetDeployMode(DEPLOYMODE.ANYWHERE)

    inst:AddComponent("hauntable")
    inst.components.hauntable:SetHauntValue(TUNING.HAUNT_TINY)

    inst:AddComponent("bboy_upgrade")

    MakeSmallBurnable(inst, 30)
    MakeSmallPropagator(inst)

    inst.OnSave = item_OnSave
    inst.OnLoad = item_OnLoad

    return inst
end

return Prefab("bboy_candle01", fn01, assets, prefabs),
    Prefab("bboy_candle02", fn02, assets, prefabs),
    Prefab("bboy_candle03", fn03, assets, prefabs),
    MakePlacer("bboy_candle_item_placer", "bboy_candle", "bboy_candle", "idle01"),
    Prefab("bboy_candle_item", itemfn, assets, prefabs_item)
